package com.ytjj.qmyx.mall.controller.h5;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.response.*;
import com.ytjj.qmyx.mall.service.ActivityService;
import com.ytjj.qmyx.mall.service.CommonService;
import com.ytjj.qmyx.mall.service.EvaluationServive;
import com.ytjj.qmyx.mall.service.SpecialService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 *
 *  *
 **/
@RestController
@RequestMapping("/h5/index")
public class H5IndexController extends BaseController {
    @Resource
    private CommonService commonService;

    @Resource
    private ActivityService activityService;

    @Resource
    private SysConfigUtil sysConfigUtil;

    @Resource
    private SpecialService specialService;

    @Resource
    private RedisService redisService;

    /**
     * h5 生活馆
     */
    @GetMapping("discover")
    public CommonResult getDiscoverPage() {
        H5DiscoverResponse h5DiscoverResponse=new H5DiscoverResponse();
        ForumLiveResponse forumLiveResponse=commonService.getH5ForumLiveData();
        h5DiscoverResponse.setForumLiveList(forumLiveResponse.getLiveInfoList());
        List<BannersResponse> bannersList = commonService.getBannersListByType("LIFE_HOUSE");
        // 首页专区列表
        List<SpecialResponse> specialList = specialService.getSpecialListByType("mall", 1);
        // 秒杀
        List<ProductResponse> seckillList = commonService.querySeckillList();
        h5DiscoverResponse.setSeckillList(seckillList);
        h5DiscoverResponse.setSpecialList(specialList);
        h5DiscoverResponse.setBannersList(bannersList);
        // 用户积分
        UsersResponse h5CurrentUser = this.getH5CurrentUser();
        if(h5CurrentUser == null){
            h5DiscoverResponse.setScore(Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.DEFAULT_SCORE)));
        }else{
            h5DiscoverResponse.setScore(h5CurrentUser.getScore());
        }
        // 用户红包
        ReturnCouponResponse returnCouponResponse = this.getReturnCoupon(RedisConstants.RETURN_COUPON + TokenUtil.getToken());
        h5DiscoverResponse.setActivityMax(Optional.ofNullable(returnCouponResponse.getActivityMax()).orElse(new BigDecimal(680)));
        return CommonResult.success(h5DiscoverResponse);
    }

    /**
     * h5 首页
     */
    @GetMapping("index")
    public CommonResult getIndexPage(@RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {
        H5IndexResponse h5IndexResponse=new H5IndexResponse();
        //卡券
        h5IndexResponse.setScoreProductList(activityService.getActivityList(1,9, ActivityTypeEnums.CASH_SCORE.name(),null));
        //特卖
        h5IndexResponse.setSeckillProductList(activityService.getActivityList(1,3, ActivityTypeEnums.SECKILL.name(),null));
        //补贴
        h5IndexResponse.setOldForNewProductList(activityService.queryOldForNewList(0,1,3,flag));
        //返话费
        h5IndexResponse.setRechargeProductList(activityService.getActivityList(1,9, ActivityTypeEnums.RECHARGE_COUPON.name(),null));
        //0元测评
        h5IndexResponse.setZeroProductList(activityService.getActivityList(1,9, ActivityTypeEnums.ZERO_EVALUATION.name(),null));
        //先用后付
        h5IndexResponse.setPayAfterUseProductList(activityService.getActivityList(1,9, ActivityTypeEnums.PAY_AFTER_USE.name(),null));
        // 用户积分
        UsersResponse h5CurrentUser = this.getH5CurrentUser();
        if(h5CurrentUser == null){
            h5IndexResponse.setScore(Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.DEFAULT_SCORE)));
        }else{
            h5IndexResponse.setScore(h5CurrentUser.getScore());
        }
        // 用户红包
        ReturnCouponResponse returnCouponResponse = this.getReturnCoupon(RedisConstants.RETURN_COUPON + TokenUtil.getToken());
        h5IndexResponse.setActivityMax(Optional.ofNullable(returnCouponResponse.getActivityMax()).orElse(new BigDecimal(680)));
        return CommonResult.success(h5IndexResponse);
    }

    private ReturnCouponResponse getReturnCoupon(String key) {
        ReturnCouponResponse returnCouponResponse = new ReturnCouponResponse();

        if (StringUtils.isNotEmpty(redisService.get(key))) {// 记录用户返现红包金额
            returnCouponResponse = JSONObject.parseObject(redisService.get(key), ReturnCouponResponse.class);
        }

        returnCouponResponse.setUsedActivityMax(Optional.ofNullable(returnCouponResponse.getUsedActivityMax())
                .orElse(BigDecimal.ZERO).setScale(0, BigDecimal.ROUND_HALF_UP));
        returnCouponResponse.setTotalReturnCoupon(Optional.ofNullable(returnCouponResponse.getTotalReturnCoupon())
                .orElse(BigDecimal.ZERO).setScale(0, BigDecimal.ROUND_HALF_UP));
        returnCouponResponse.setActivityMax(Optional.ofNullable(returnCouponResponse.getActivityMax())
                .orElse(BigDecimal.valueOf(680)).setScale(0,BigDecimal.ROUND_HALF_UP));
        return returnCouponResponse;
    }

    /**
     * 获取当前渠道是否可以跳快应用
     * @param channel
     * @return
     */
    @GetMapping("channelKyy")
    public CommonResult getChannelKyy(@RequestParam String channel) {
        String channel_kyy = redisService.getMapString(RedisConstants.SYS_CONFIG, "CHANNEL_KYY");
        return CommonResult.success(channel_kyy.contains(channel));
    }

}
